package com.sin.j2me.wihd.window;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;

import com.sin.j2me.common.TextFieldStyles;
import com.sin.j2me.framework.SinFramework;
import com.sin.j2me.update.CheckUpdate;
import com.sin.j2me.update.UpdateObject;
import com.sin.j2me.update.WhenUpdate;
import com.sin.j2me.util.AsynchTask;
import com.sin.j2me.wihd.FrameWork;
import com.sin.j2me.wihd.common.Commands;
import com.sin.j2me.wihd.common.Constant;
import com.sin.j2me.wihd.common.Protocol;
import com.sin.j2me.wihd.common.Strings;
import com.sin.j2me.wihd.communi.InterfaceRequest;
import com.sin.j2me.wihd.model.Param;
import com.sin.j2me.wihd.model.Response;
import com.sin.j2me.wihd.version.AppInfo;

public class LoginWindow extends Window implements CommandListener {
	private Form form = null;
	private TextField username = null;
	private TextField password = null;
	public LoginWindow() {		
		this.form = new Form(Strings.login);
		this.form.addCommand(Commands.exit);
		this.form.addCommand(Commands.update);
		this.form.addCommand(Commands.signin);
		this.form.addCommand(Commands.login);
		
		username = new TextField(Strings.username, UO.getUsername(), Constant.LEN_USERNAME, TextFieldStyles.USERNAME);
		password = new TextField(Strings.password, UO.getPassword(), Constant.LEN_PASSWORD, TextFieldStyles.PASSWORD);
		
		form.append(username);
		form.append(password);

		form.setCommandListener(this);
	}
	public Displayable getWindow() {
		return form;
	}
	public void commandAction(Command cmd, Displayable dis) {
//		System.out.println("Cmd:" + cmd.getLabel());
		if(cmd == Commands.login){
			// Login
			if(locked){
				setNotice(Strings.I_LOCKED);
				return;
			}
			if(username.getString().length() == 0 || password.getString().length()==0){
				setNotice(Strings.N_EMPTY_USERORPWD);
				return;
			}
			UO.setUsername(username.getString());
			UO.setPassword(password.getString());
			setLock(true);
			new AsynchTask(new Runnable() {
				public void run() {
					Param param = new Param();
					param.addPam(Protocol.P_USERNAME, UO.getUsername());
					param.addPam(Protocol.P_PASSWORD, UO.getPassword());
					Response response = InterfaceRequest.getResponse(Protocol.I_LOGIN, param);
					
					if(response.isOk()){
						String tkn = response.getString(Protocol.P_TOKEN);
						String uid = response.getString(Protocol.P_USERID);
						
						if(tkn != null && uid != null){
							int id = Integer.parseInt(uid);
							UO.setUserid(id);
							UO.setToken(tkn);
						}

						FrameWork.startWindow(FrameWork.thingsWindow);
						FrameWork.thingsWindow.getLatestThings();
						setNotice(Strings.N_LOGINOK);
					}
					else if(response.getStatus() == Response.FAIL){
						setNotice(response.getString(Protocol.R_INFO));
					}
					else{
						setNotice(Strings.P_NETWORKERR);
					}
					setLock(false);
				}
			});
		}
		else if(cmd == Commands.exit){
			// Exit
			FrameWork.exit();
		}
		else if(cmd == Commands.signin){
			// Sign in
			FrameWork.startWindow(FrameWork.signinWindow);
		}
		else if(cmd == Commands.update){
			// Update
			CheckUpdate.checkUpdate(AppInfo.updateid, AppInfo.version, new WhenUpdate() {				
				public boolean whenUpdate(UpdateObject update) {
					if(update != null){
						setNotice(update.toString());
						SinFramework.openWeb(update.getUrl());
					}
					else
						setNotice("null");
					return false;
				}
			});
		}
	}
}
